﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeBuilder.Repository
{
    internal class CreateBaseRepository
    {
        private string tableName;
        private string fileDir;
        public CreateBaseRepository(string tableName, string fileDir)
        {
            this.tableName = tableName;
            this.fileDir = fileDir;
        }

        public void Run()
        {
            var formatTableName = Helper.GetPascalCase(tableName);
            var entityName = $"{formatTableName}Entity";
            var fileName = "BaseRepository";
            IList<string> contents = new List<string>();
            contents.Add($"using Hualong.TestWorldPro.Entities;");
            contents.Add($"using Microsoft.Extensions.Logging;");
            contents.Add($"namespace Hualong.TestWorldPro.Repository.{formatTableName}");
            contents.Add("{");
            contents.Add($"    public class BaseRepository : IBaseRepository");
            contents.Add("    {");
            contents.Add($"        public static SQLiteAsyncConnection SqliteDb = new SQLiteAsyncConnection(DBHelper.DatabasePath, DBHelper.Flags);");
            contents.Add($"        public readonly ILogger<BaseRepository> logger;");
            contents.Add($"        public BaseRepository(ILogger<BaseRepository> logger)");
            contents.Add("        {");
            contents.Add($"            this.logger = logger;");
            contents.Add($"            logger.LogInformation($\"Database Path={{DBHelper.DatabasePath}}\");");
            contents.Add($"            if (SqliteDb is not null)");
            contents.Add($"                return;");
            contents.Add($"            if (!File.Exists(DBHelper.DatabasePath))");
            contents.Add("            {");
            contents.Add($"                logger.LogInformation($\"{{DBHelper.DatabasePath}} isn't existed\");");
            contents.Add($"                using (Stream inputStream = FileSystem.Current.OpenAppPackageFileAsync(DBHelper.DatabaseFilename).Result)");
            contents.Add("                {");
            contents.Add($"                    using FileStream outputStream = File.Create(DBHelper.DatabasePath);");
            contents.Add($"                    inputStream.CopyToAsync(outputStream);");
            contents.Add("                }");
            contents.Add("            }");
            contents.Add("            SqliteDb = new SQLiteAsyncConnection(DBHelper.DatabasePath, DBHelper.Flags);");
            contents.Add("        }");
            contents.Add("        public async Task<int> DeleteItemAsync(object item)");
            contents.Add("        {");
            contents.Add("            return await SqliteDb.DeleteAsync(item);");
            contents.Add("        }");
            contents.Add("        public async Task<int> DeleteItemsAsync(IEnumerable items)");
            contents.Add("        {");
            contents.Add("            int count = 0;");
            contents.Add("            foreach (var item in items)");
            contents.Add("            {");
            contents.Add("                var result = await SqliteDb.DeleteAsync(item);");
            contents.Add("                if (result > 0) count++;");
            contents.Add("            }");
            contents.Add("            return count;");
            contents.Add("        }");
            contents.Add("        public async Task<int> InsertAsync(object item)");
            contents.Add("        {");
            contents.Add("            return await SqliteDb.InsertAsync(item);");
            contents.Add("        }");
            contents.Add("        public async Task<int> InsertAllAsync(IEnumerable items)");
            contents.Add("        {");
            contents.Add("            return await SqliteDb.InsertAllAsync(items);");
            contents.Add("        }");
            contents.Add("        public async Task<int> UpdateAsync(object item)");
            contents.Add("        {");
            contents.Add("            return await SqliteDb.UpdateAsync(item);");
            contents.Add("        }");
            contents.Add("        public async Task<int> UpdateAllAsync(IEnumerable items)");
            contents.Add("        {");
            contents.Add("            return await SqliteDb.UpdateAllAsync(items);");
            contents.Add("        }");
            contents.Add("    }");
            contents.Add("}");
            string filePath = Path.Combine(fileDir, fileName);
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath = Path.Combine(filePath, $"{fileName}.cs");
            if (File.Exists(filePath))
            { File.Delete(filePath); }
            File.WriteAllLines(filePath, contents, Encoding.UTF8);

            Console.WriteLine($"创建:{filePath} 完成！");
        }
    }
}
